<template>
  <div class="app-container">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>广告管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-row style="position: absolute;right: 70px;z-index: 99;">
        <el-button size="small " type="danger" @click="handleAdd">添加广告</el-button>
      </el-row>
      <div style="position: absolute;left: 620px;margin-top: -8px; z-index: 99;">
        <el-input placeholder="请输入广告名称"  class="input-with-select">
          <el-select v-model="select" slot="prepend" placeholder="请选择">
            <el-option label="全部广告" value="1"></el-option>
            <el-option label="上线" value="2"></el-option>
            <el-option label="下线" value="3"></el-option>
          </el-select>
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
      <el-tabs v-model="message" @tab-click="onUserList">
        <el-tab-pane :label="`全部(${unread.length})`" name="first">
        </el-tab-pane>
        <el-tab-pane :label="`弹窗广告(${read.length})`" name="second">
        </el-tab-pane>
        <el-tab-pane :label="`首页轮播广告(${recycle.length})`" name="third">
        </el-tab-pane>
        <!--列表-->
        <el-table :data="users" highlight-current-row @selection-change="selsChange" style="width: 100%;">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column prop="shopId" label="编号" width="150">
          </el-table-column>
          <el-table-column prop="time" label="开始时间" width="160">
          </el-table-column>
          <el-table-column prop="time" label="结束时间" width="160">
          </el-table-column>
          <el-table-column prop="name" label="广告名称" width="180">
          </el-table-column>
          <el-table-column prop="name" label="广告位置" width="150">
          </el-table-column>
          <el-table-column prop="name" label="广告图片" width="130">
            <template slot-scope="scope">
              <img  :src="scope.row.img" alt="" style="width: 50px;height: 50px">
            </template>
          </el-table-column>
          <el-table-column prop="mon" label="广告状态" width="130">
          </el-table-column>
          <el-table-column prop="nnn" label="广告排序" width="130">
          </el-table-column>
          <el-table-column prop="nnn" label="点击次数" min-width="120">
          </el-table-column>
          <el-table-column label="操作" width="220">
            <template slot-scope="scope">
              <el-button size="mini" @click="goOnline">上线</el-button>
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">维护</el-button>
              <el-button type="danger" size="mini" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

       <!-- 维护弹出表单 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="广告名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" placeholder="请输入广告名称" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="广告位置" :label-width="formLabelWidth">
              <el-select v-model="form.region" placeholder="请选择广告位置">
                <el-option label="首页轮播" value="shanghai"></el-option>
                <el-option label="弹窗广告" value="beijing"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="广告排序" :label-width="formLabelWidth">
              <el-select v-model="form.region1" placeholder="请选择广告位置">
                <el-option label="1" value="shanghai"></el-option>
                <el-option label="2" value="beijing"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="开始时间" :label-width="formLabelWidth">
                <el-date-picker v-model="value1" type="date" placeholder="选择开始日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" :label-width="formLabelWidth">
                <el-date-picker v-model="value2" type="date" placeholder="选择结束日期"></el-date-picker>
            </el-form-item>

            <el-form-item label="广告图片" :label-width="formLabelWidth">
              <el-upload
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-remove="handleRemove">
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>

            </el-form-item>
            <el-form-item label="广告链接" :label-width="formLabelWidth">
              <el-input v-model="form.lianjie" placeholder="请输入广告链接地址" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="广告备注" :label-width="formLabelWidth">
                <el-input type="textarea" placeholder="请输入广告描述备注信息" :autosize="{ minRows: 2}" maxlength="1024" show-word-limit></el-input>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button v-if="dialogStatus=='create'" type="primary">添加</el-button>
            <el-button v-else type="primary">修改</el-button>
          </div>
        </el-dialog>

        <!--工具条-->
        <el-col :span="24" class="toolbar" style="margin-top: 25px;">
          <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
          <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total"
            style="float:right;">
          </el-pagination>
        </el-col>

      </el-tabs>
    </div>
    <div>
    </div>
  </div>
</template>

<script>
  import {
    removeUser,
    batchRemoveUser
  } from '@/api/userTable'

  export default {
    data() {
      return {
        message: 'first',
        dialogStatus: '',
        textMap: {
          update: '编辑广告内容',
          create: '添加新广告'
        },
        unread: [{}, {}],
        read: [{}],
        recycle: [{}],
        users: [],
        sels: [],
        total: 0,
        select: '',
        stateNum: 1,
        dialogFormVisible: false,
        value1: '',
        value2: '',
        form: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          region1: '',
          lianjie: '',
          desc: ''
        },
        formLabelWidth: '120px',
        dialogImageUrl: '',
        dialogVisible: false
      }
    },
    methods: {
      onUserList(tab) {
        console.log('---3232323232', tab, this.stateNum)
        var tap = tab === 1 ? 'first' : tab.name
        console.log('---==========================', tap)
        var n = this.stateNum + 1
        var list = [{
          name: n,
          shopId: 202003049958775,
          img: '../static/img/img.jpg',
          nnn: 6666,
          mon: 150,
          time: '2020-03-02 15:00:00'
        }]
        this.users = list
        this.stateNum = n
        this.total = list.length
      },
      // 显示编辑界面
      handleEdit(index, row) {
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        // this.editForm = Object.assign({}, row)
      },
      // 显示新增界面
      handleAdd() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        // this.editForm = {
        //   id: '0',
        //   name: '',
        //   sex: 1,
        //   age: 0,
        //   birth: '',
        //   addr: ''
        // }
      },
      handleRemove(file, fileList) {
        console.log(file, fileList)
      },
      // 全选单选多选
      selsChange(sels) {
        this.sels = sels
      },
      // 上线
      goOnline(index) {
        this.$confirm('确认立即发布上线？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      },
      // 删除
      handleDel(index, row) {
        this.$confirm('确认删除当前广告？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            const para = {
              id: row.id
            }
            removeUser(para).then(res => {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getUsers()
            })
          })
          .catch(() => {})
      },
      handleCurrentChange(val) {
        this.page = val
        this.getUsers()
      },
      // 批量删除
      batchRemove() {
        var ids = this.sels.map(item => item.id).toString()
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        })
          .then(() => {
            const para = {
              ids: ids
            }
            batchRemoveUser(para).then(res => {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getUsers()
            })
          })
          .catch(() => {})
      }
    },
    mounted() {
      this.onUserList(1)
    }
  }
</script>

<style>
  .message-title {
    cursor: pointer;
  }

  .handle-row {
    margin-top: 30px;
  }
 .el-select .el-input {
    width: 200px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
 .content-title {
    font-weight: 400;
    line-height: 50px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
  }
</style>
